package com.scm.service.impl;

import java.util.Date;
import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.scm.common.core.domain.entity.SysUser;
import com.scm.common.core.page.PageDomain;
import com.scm.common.utils.DateUtils;
import com.scm.common.utils.StringUtils;
import com.scm.common.utils.sql.SqlUtil;
import com.scm.converter.PurchaseConverter;
import com.scm.dto.req.purchase.SaveScmPurchaseReq;
import com.scm.dto.resp.PurchasePageDo;
import com.scm.dto.resp.QueryPurchasePageResp;
import com.scm.persist.model.ScmPurchasePayment;
import com.scm.persist.model.ScmPurchasePaymentExample;
import com.scm.persist.model.ScmPurchaseQuotationDetail;
import com.scm.persist.model.ScmPurchaseQuotationDetailExample;
import com.scm.persist.model.purchase.ScmPurchase;
import com.scm.persist.model.purchase.ScmPurchaseExample;
import com.scm.persist.model.purchase.ScmPurchaseInventoryDetail;
import com.scm.persist.model.purchase.ScmPurchaseInventoryDetailExample;
import com.scm.persist.mybatis.ScmPurchasePaymentMapper;
import com.scm.persist.mybatis.ScmPurchaseQuotationDetailMapper;
import com.scm.persist.mybatis.purchase.ScmPurchaseInventoryDetailMapper;
import com.scm.persist.mybatis.purchase.ScmPurchaseMapper;
import com.scm.service.IScmPurchaseService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 采购Service业务层处理
 *
 * @author reptilemanager
 * @date 2024-03-24
 */
@Service
public class ScmPurchaseServiceImpl implements IScmPurchaseService
{
    @Autowired
    private ScmPurchaseMapper scmPurchaseMapper;

    @Autowired
    private ScmPurchaseInventoryDetailMapper scmPurchaseInventoryDetailMapper;

    @Autowired
    private ScmPurchaseQuotationDetailMapper scmPurchaseQuotationDetailMapper;

    @Autowired
    private ScmPurchasePaymentMapper scmPurchasePaymentMapper;


    /**
     * 查询采购
     *
     * @param id 采购主键
     * @return 采购
     */
    @Override
    public ScmPurchase selectScmPurchaseById(Long id)
    {
        return scmPurchaseMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询采购列表
     *
     * @param scmPurchase 采购
     * @return 采购
     */
    @Override
    public List<ScmPurchase> selectScmPurchaseList(ScmPurchase scmPurchase)
    {
        ScmPurchaseExample example = new ScmPurchaseExample();

        return scmPurchaseMapper.selectByExample(example);
    }



    @Override
    public QueryPurchasePageResp selectScmPurchaseList(ScmPurchase scmPurchase, PageDomain pageDomain,SysUser sysUser) {

        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
        Boolean reasonable = pageDomain.getReasonable();
        PageHelper.startPage(pageNum, pageSize, orderBy).setReasonable(reasonable);
        ScmPurchaseExample example = new ScmPurchaseExample();
        ScmPurchaseExample.Criteria criteria = example.createCriteria();
        criteria.andPurchaseOrderTypeEqualTo("0").andTenantIdEqualTo(sysUser.getTenantId());

        List<ScmPurchase> scmPurchases = scmPurchaseMapper.selectByExample(example);

        QueryPurchasePageResp rspData = new QueryPurchasePageResp();
        List<PurchasePageDo> purchasePageDos = PurchaseConverter.convertScmOrdersToOrderPages(scmPurchases);
        rspData.setList(purchasePageDos);
        rspData.setTotle(new PageInfo(scmPurchases).getTotal());
        return rspData;
    }

    /**
     * 新增采购
     *
     * @param req 采购
     * @return 结果
     */
    @Override
    public int insertScmPurchase(SaveScmPurchaseReq  req, SysUser sysUser)
    {
        ScmPurchase scmPurchase = req.getScmPurchase();
        scmPurchase.setPurchaseOrderType(String.valueOf(0));
        scmPurchase.setCreateTime(System.currentTimeMillis());
        scmPurchase.setCreateUser(sysUser.getUserName());
        scmPurchase.setCreateUserId(String.valueOf(sysUser.getUserId()));
        scmPurchase.setTenantId(sysUser.getTenantId());



        if(StringUtils.isNotBlank(scmPurchase.getPurchaseDateStr())){
            Date orderTime = DateUtils.parseDateByExcel(scmPurchase.getPurchaseDateStr());
            scmPurchase.setPurchaseDate(orderTime.getTime());
        }else {
            scmPurchase.setPurchaseDate(System.currentTimeMillis());
        }


        int i = scmPurchaseMapper.insertSelective(scmPurchase);
        installscmInventoryInDetailList(req, sysUser, scmPurchase);
        return i;
    }

    private void installscmInventoryInDetailList(SaveScmPurchaseReq req, SysUser sysUser, ScmPurchase scmPurchase) {
        List<ScmPurchaseQuotationDetail> scmPurchaseQuotationDetailList = req.getScmPurchaseQuotationDetailList();
        // todo list 插入

        if(!CollectionUtils.isEmpty(scmPurchaseQuotationDetailList)){
            for (ScmPurchaseQuotationDetail scmPurchaseInventoryDetail : scmPurchaseQuotationDetailList) {
                scmPurchaseInventoryDetail.setPurchaseId(scmPurchase.getId());
                scmPurchaseInventoryDetail.setCreateTime(System.currentTimeMillis());
                scmPurchaseInventoryDetail.setCreateUser(String.valueOf(sysUser.getUserId()));
                scmPurchaseInventoryDetail.setTenantId(sysUser.getTenantId());
                // todo 批量
                //scmPurchaseInventoryDetailMapper.insert(scmPurchaseInventoryDetail);
                scmPurchaseQuotationDetailMapper.insert(scmPurchaseInventoryDetail);
            }
        }
    }

    /**
     * 修改采购
     *
     * @param req 采购
     * @return 结果
     */
    @Override
    public int updateScmPurchase(SaveScmPurchaseReq req,SysUser sysUser)
    {
        ScmPurchase scmPurchase = req.getScmPurchase();
        scmPurchase.setUpdateTime(System.currentTimeMillis());
        scmPurchase.setUpdateUser(sysUser.getUserName());
        scmPurchase.setUpdateUserId(String.valueOf(sysUser.getUserId()));


        /*ScmPurchaseInventoryDetailExample example = new ScmPurchaseInventoryDetailExample();
        ScmPurchaseInventoryDetailExample.Criteria criteria = example.createCriteria();
        criteria.andInventoryInIdEqualTo(scmPurchase.getId());
        scmPurchaseInventoryDetailMapper.deleteByExample(example);*/

        ScmPurchaseQuotationDetailExample example = new ScmPurchaseQuotationDetailExample();
        ScmPurchaseQuotationDetailExample.Criteria criteria1 = example.createCriteria();
        criteria1.andPurchaseIdEqualTo(scmPurchase.getId()).andTenantIdEqualTo(sysUser.getTenantId());
        scmPurchaseQuotationDetailMapper.deleteByExample(example);

        installscmInventoryInDetailList(req, sysUser, scmPurchase);
        return scmPurchaseMapper.updateByPrimaryKeySelective(scmPurchase);
    }

    /**
     * 批量删除采购
     *
     * @param ids 需要删除的采购主键
     * @return 结果
     */
    @Override
    public int deleteScmPurchaseByIds(String ids)
    {
        ScmPurchaseExample example = new ScmPurchaseExample();
        return scmPurchaseMapper.deleteByExample( example);
    }

    /**
     * 删除采购信息
     *
     * @param id 采购主键
     * @return 结果
     */
    @Override
    public int deleteScmPurchaseById(Long id)
    {
        return scmPurchaseMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<ScmPurchaseQuotationDetail> selectScmPurchaseInventoryDetailList(Long id) {
        // ScmPurchaseInventoryDetailExample example = new ScmPurchaseInventoryDetailExample();
        //ScmPurchaseInventoryDetailExample.Criteria criteria = example.createCriteria();
        //criteria.andInventoryInIdEqualTo(id);
        //List<ScmPurchaseInventoryDetail> scmPurchaseInventoryDetails = scmPurchaseInventoryDetailMapper.selectByExample(example);
        ScmPurchaseQuotationDetailExample example = new ScmPurchaseQuotationDetailExample();
        ScmPurchaseQuotationDetailExample.Criteria criteria1 = example.createCriteria();
        criteria1.andPurchaseIdEqualTo(id);
        return scmPurchaseQuotationDetailMapper.selectByExample(example);
    }


    @Override
    public void uploadPaymentVoucherPost(Long id, SysUser sysUser, String url) {


        ScmPurchasePaymentExample example = new ScmPurchasePaymentExample();
        ScmPurchasePaymentExample.Criteria criteria = example.createCriteria().andPurchaseIdEqualTo(id);
        List<ScmPurchasePayment> scmPurchasePayments = scmPurchasePaymentMapper.selectByExample(example);

        ScmPurchasePayment scmPurchasePayment = new ScmPurchasePayment();
        scmPurchasePayment.setPaymentFile(url);
        scmPurchasePayment.setUploadPaymentFileTime(System.currentTimeMillis());
        scmPurchasePayment.setUploadPaymentFileUser(sysUser.getUserName());
        if(CollectionUtils.isNotEmpty(scmPurchasePayments)){
            //ScmPurchasePayment scmPurchasePayment1 = scmPurchasePayments.get(0);

            scmPurchasePayment.setUpdateTime(System.currentTimeMillis());
            scmPurchasePayment.setUpdateUser(sysUser.getUserName());
            ScmPurchasePaymentExample exampleUpdate = new ScmPurchasePaymentExample();
            ScmPurchasePaymentExample.Criteria criteria1 = exampleUpdate.createCriteria();
            criteria1.andPurchaseIdEqualTo(id);
            scmPurchasePaymentMapper.updateByExampleSelective(scmPurchasePayment, exampleUpdate);
        }else{
            scmPurchasePayment.setCreateTime(System.currentTimeMillis());
            scmPurchasePayment.setCreateUser(sysUser.getUserName());
            scmPurchasePayment.setPurchaseId(id);
            scmPurchasePayment.setTenantId(sysUser.getTenantId());
            scmPurchasePaymentMapper.insert(scmPurchasePayment);
        }


    }
}
